package cd.itcast.spark.kudu.kudu_jdbc

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object ImpalaKuduJdbc {
  def main(args: Array[String]): Unit = {
    var conn: Connection = null
    var ps: PreparedStatement = null
    var rs: ResultSet = null
    // 加载驱动类
    try {

      Class.forName("org.apache.hive.jdbc.HiveDriver")

      //注意这里是impala的数据库，不是mysql的数据库
      conn = DriverManager.getConnection("jdbc:impala://node02:21050/default")
      //     准备SQL  kudu_itcast  kudu_itcast
      val sql = "select * from kudu_itcast where age >20 "
      //执行查询语句,获取查询对象
      ps = conn.prepareStatement(sql)
      //通过查询对象获取查询结果
       rs = ps.executeQuery()
      // 获取数据

      //    id	0	int32 NOT NULL	AUTO_ENCODING	DEFAULT_COMPRESSION	-	-
      //    name	1	string NULLABLE	AUTO_ENCODING	DEFAULT_COMPRESSION	-	-
      //      age	2	int32 NULLABLE	AUTO_ENCODING	DEFAULT_COMPRESSION	-	-
      //      gender	3	string NULLABLE
      while (rs.next()) {
        var id = rs.getInt("id")
        var name = rs.getString("name")
        var age = rs.getInt("age")
        var gender = rs.getString("gender")
        println(id, name, age, gender)
      }
    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      if (null != rs) rs.close()
      if (null != rs) ps.close()
      if (null != rs) conn.close()

    }


  }

}
